package com.zxkj.commonpatrol.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.allen.library.RxHttpUtils;
import com.allen.library.base.BaseHttpUtil;
import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.czy.basecommon.utils.tools.FileHelper;
import com.czy.basecommon.utils.tools.GsonUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.toast.XToast;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.FzAdapt;
import com.zxkj.commonpatrol.adapt.FzTestAdapt;
import com.zxkj.commonpatrol.adapt.FzTestFinalAdapt;

import com.zxkj.commonpatrol.adapt.MyDiffCallback;
import com.zxkj.commonpatrol.databinding.ActivityShowMediaBinding;
import com.zxkj.commonpatrol.model.entity.GroupMember;
import com.zxkj.commonpatrol.model.entity.itemPicEntity;
import com.zxkj.commonpatrol.model.entity.itemVioceEntity;
import com.zxkj.commonpatrol.model.livedata.DiffBean;
import com.zxkj.commonpatrol.model.response.MediaInfoResponse;
import com.zxkj.commonpatrol.viewmodel.ShowMediaDetailViewModel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShowMediaActivity extends BaseVmDataBindingActivity<ActivityShowMediaBinding, ShowMediaDetailViewModel> {


    private String alarmId;
    private MiniLoadingDialog miniLoadingDialog;
    private boolean isHasPicVioce=false;
    private List<MediaInfoResponse.MediaVoice> voice;
    private GroupMember groupMember1;
    private GroupMember groupMember2;
    private GroupMember groupMember3;
    private   List<GroupMember> groupMembers;
    private FzTestFinalAdapt adapt;
    private String name;
    private boolean isLoading=true;
    private Map<Integer, MediaInfoResponse.MediaPhoto> maps;
    private MiniLoadingDialog loadingDialog;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_show_media;
    }

    @Override
    public int initVariableId() {
        return BR.ShowMediaViewModel;
    }

    @Override
    protected void initUI() {
        alarmId = getIntent().getStringExtra("AlarmId");
         name = getIntent().getStringExtra("name");
        String time = getIntent().getStringExtra("time");
        if (TextUtils.isEmpty(time)){
            binding.llHead.setVisibility(View.GONE);
        }else{
            binding.llHead.setVisibility(View.VISIBLE);
            binding.tvHead.setText(time);
        }
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        binding.recy.setLayoutManager(layoutManager);
         loadingDialog =new MiniLoadingDialog(this,"加載中...");
        loadingDialog.show();
        Data();
    }

    @Override
    protected void initData() {

    }


    public void Data() {
          if (name!=null){
              binding.tvTitle.setText(name);
          }

        viewModel.getAlarmLogMedioInfo(alarmId);

    }

    @Override
    protected void setListener() {
        super.setListener();
        binding.qkBack.setOnClickListener(v->{
            if (!isLoading){
                ActivityTool.getInstance().popOneActivity(this);
                FileHelper.deleteFiles(new File(ItrackConstant.DownPicture));
            }

        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxHttpUtils.cancel("down");
    }
    @Override
    public void onBackPressed() {
      if (!isLoading){
          super.onBackPressed();
          FileHelper.deleteFiles(new File(ItrackConstant.DownPicture));

      }
    }
    @Override
    protected void dataObserver() {
        super.dataObserver();
         LiveEventBus.get("dif", DiffBean.class).observe(this, new Observer<DiffBean>() {
             @Override
             public void onChanged(DiffBean diffBean) {
                 adapt.setDiffNewData(diffBean.getCopyData());
             }
         });
        viewModel.MediaInfoResponses.observe(this, new Observer<StateData<List<MediaInfoResponse>>>() {
            @Override
                public void onChanged(StateData<List<MediaInfoResponse>> mediaInfoResponseStateData) {
                      isLoading=false;
                    if (mediaInfoResponseStateData.getStatus()== StateData.DataStatus.SUCCESS){
                        if (mediaInfoResponseStateData.getData()!=null){
                            if (mediaInfoResponseStateData.getData().size()>0){

                                List<MediaInfoResponse> data = mediaInfoResponseStateData.getData();
                                for (int i = 0; i < data.size(); i++) {
                                    data.get(i).setId(i);
                                }
                                  maps=new HashMap<Integer, MediaInfoResponse.MediaPhoto>();
                                 for (int i = 0; i < data.size(); i++) {

                                    if (data.get(i).getPhoto()!=null&&data.get(i).getPhoto().size()>0){
                                        //说明有图片 去拿第一张信息下载
                                        MediaInfoResponse.MediaPhoto mediaPhoto = data.get(i).getPhoto().get(0);
                                        maps.put(i,mediaPhoto);

                                    }
                                }
                                   adapt =new FzTestFinalAdapt(viewModel,R.layout.text,data,loadingDialog);
                                adapt.setDiffCallback(new MyDiffCallback());
                                   binding.recy.setAdapter(adapt);
                                   viewModel.mRepository.DownPo(maps, new BaseRepository.MyBaseDataCallBack<Map<Integer, String>>() {
                                     @Override
                                     public void onDataSuccessCallBack(Map<Integer, String> integerStringMap) {

                                         runOnUiThread(new Runnable() {
                                             @Override
                                             public void run() {
                                                 if ( loadingDialog!=null){
                                                     loadingDialog.dismiss();
                                                 }
                                                 List<MediaInfoResponse> data1 = adapt.getData();
                                                 String s = GsonUtil.getGson().toJson(data1);
                                                 List<MediaInfoResponse> CopyData = new Gson().fromJson(s, new TypeToken<List<MediaInfoResponse>>(){}.getType());
                                                 for (Map.Entry<Integer, String> entry : integerStringMap.entrySet()) {
                                                     CopyData.get(entry.getKey()).setFristPath(entry.getValue());
                                                 }
                                                 adapt.setDiffNewData(CopyData);

//                                                 MyDiffCallback callback = new MyDiffCallback(data1, CopyData);
//                                                 adapt.setDiffCallback(callback);
//                                                 adapt.setDiffNewData(CopyData);
//                                                 MyDiffCallback callback = new MyDiffCallback(data1, CopyData);
//
//                                                 DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
//
//                                                 result.dispatchUpdatesTo(adapt);



                                             }
                                         });


                                     }

                                     @Override
                                     public void onDataFailCallBack(Map<Integer, String> integerStringMap, Map<Integer, String> t1) {
                                         if ( loadingDialog!=null){
                                             loadingDialog.dismiss();
                                         }
                                     }
                                 });
//                                //去下载首张图片
//                                viewModel.mRepository.DownLoadPhoto("", ItrackConstant.Picture, "", 0, 0, data.getPhoto(), new BaseRepository.MyBaseDataCallBack() {
//                                    @Override
//                                    public void onDataSuccessCallBack(Object o) {
//                                        List<itemPicEntity> itemPicEntityList = (List<itemPicEntity>) o;
//
//                                    }
//
//                                    @Override
//                                    public void onDataFailCallBack(Object o, Object t1) {
//
//                                    }
//                                });

                            }else {
                                if ( loadingDialog!=null){
                                    loadingDialog.dismiss();
                                }
                            }
                        }else{
                            if ( loadingDialog!=null){
                                loadingDialog.dismiss();
                            }
                        }
                    }else{
                        if ( loadingDialog!=null){
                            loadingDialog.dismiss();
                        }
                    }
        }
    });


//        viewModel.picData.observe(this, new Observer<StateData<List<itemPicEntity>>>() {
//            @Override
//            public void onChanged(StateData<List<itemPicEntity>> objectStateData) {
//
//                          if (objectStateData.getData()!=null){
//                              if (objectStateData.getData().containsKey(1)){
//                                  groupMember1=new GroupMember();
//                                  groupMember1.setPicData(objectStateData.getData().get(1));
//                              }else if (objectStateData.getData().containsKey(2)){
//                                  groupMember2=new GroupMember();
//                                  groupMember2.setPicData(objectStateData.getData().get(2));
//                              }else{
//                                  groupMember3=new GroupMember();
//                                  groupMember3.setPicData(objectStateData.getData().get(3));
//                              }
//
//
//
//                          }
//
//                if (voice != null && voice.size() > 0) {
//                    viewModel.downLoadVoice("", ItrackConstant.VioceDir, "", 0, 0,voice);
//                }else{
//                    if (miniLoadingDialog != null) {
//                        miniLoadingDialog.dismiss();
//                    }
//                 groupMembers=new ArrayList<>();
//                    //设置数据
//                    if (groupMember1!=null){
//                        groupMembers.add(groupMember1);
//                    }
//                    if (groupMember2!=null){
//                        groupMembers.add(groupMember2);
//                    }
//                    if (groupMember3!=null){
//                        groupMembers.add(groupMember3);
//                    }
//                    FzAdapt fzAdapt=new FzAdapt(R.layout.text,groupMembers);
//                    binding.recy.setAdapter(fzAdapt);
//                }
//            }
//        });
//        viewModel.viceData.observe(this, new Observer<StateData<HashMap<Integer,List<itemVioceEntity>>>>() {
//            @Override
//            public void onChanged(StateData<HashMap<Integer,List<itemVioceEntity>>> objectStateData) {
//                if (miniLoadingDialog != null) {
//                    miniLoadingDialog.dismiss();
//                }
//                if (objectStateData.getData()!=null){
//                    if (objectStateData.getData().containsKey(1)){
//                        groupMember1=new GroupMember();
//                        groupMember1.setVioceData(objectStateData.getData().get(1));
//                    }else if (objectStateData.getData().containsKey(2)){
//                        groupMember2=new GroupMember();
//                        groupMember2.setVioceData(objectStateData.getData().get(2));
//                    }else{
//                        groupMember3=new GroupMember();
//                        groupMember3.setVioceData(objectStateData.getData().get(3));
//                    }
//                   if (groupMembers==null){
//                       groupMembers=new ArrayList<>();
//                   }
//                    //设置数据
//                    if (groupMember1!=null){
//                        groupMembers.add(groupMember1);
//                    }
//                    if (groupMember2!=null){
//                        groupMembers.add(groupMember2);
//                    }
//                    if (groupMember3!=null){
//                        groupMembers.add(groupMember3);
//                    }
//                    FzAdapt fzAdapt=new FzAdapt(R.layout.text,groupMembers);
//                    binding.recy.setAdapter(fzAdapt);
//                }
//            }
//        });
}
}
